package com.xy.smartsms.widget;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

import com.xy.smartsms.util.Log;

import java.lang.ref.WeakReference;

/**
 * 提供ListView滑动的处理帮助类，提高滑动和卡片识别性能
 */
public class ListViewScrollHelper implements OnScrollListener {
    private static final String TAG = ListViewScrollHelper.class.getSimpleName();

    private boolean mIsScrolling = false;
    private boolean mUsrScrolled = false;
    private int mLastScrollState = OnScrollListener.SCROLL_STATE_IDLE;
    private WeakReference<ListView> mListView = null;

    public ListViewScrollHelper(WeakReference<ListView> weakListView) {
        mListView = weakListView;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        Log.v(TAG, "onScroll: " + " first=" + firstVisibleItem + " visible=" + visibleItemCount);
        if (!(mUsrScrolled)
                && visibleItemCount != 0
                && (firstVisibleItem + visibleItemCount) == totalItemCount) {
            showCompleteItem(null);
        }

        if (firstVisibleItem == 0
                || firstVisibleItem + visibleItemCount == totalItemCount) {
            mIsScrolling = false;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        Log.v(TAG, "onScrollStateChanged: " + scrollState);
        switch (scrollState) {
            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                mUsrScrolled = true;
                break;

            case OnScrollListener.SCROLL_STATE_FLING:
                mUsrScrolled = true;
                mIsScrolling = true;
                break;

            case OnScrollListener.SCROLL_STATE_IDLE:
            default:
                mIsScrolling = false;
                break;
        }

        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                && mLastScrollState != OnScrollListener.SCROLL_STATE_IDLE) {
            final ListView listView = mListView.get();
            if (listView != null) {
                //Log.i(TAG, "onScrollStateChanged: notifyDataSetChanged");
                //((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
            }
        }

        mLastScrollState = scrollState;
    }

    /**
     * 获取当前滑动状态
     *
     * @return true:滑动
     */
    public boolean isScrolling() {
        return mIsScrolling;
    }

    /**
     * 在卡片切换的时候显示完整视图
     *
     * @param view 待显示的视图
     */
    public void showCompleteItem(View view) {
        final ListView listView = mListView.get();
        if (listView == null || mLastScrollState != OnScrollListener.SCROLL_STATE_IDLE) {
            return;
        }

        if (view == null) {
            // use smoothScrollToPosition() may leads to redundant onScroll()
            listView.post(new Runnable() {
                @Override
                public void run() {
                    listView.setSelection(listView.getCount() - 1);
                }
            });
        } else {
            int listPos[] = new int[2];
            listView.getLocationInWindow(listPos);
            int itemPos[] = new int[2];
            view.getLocationInWindow(itemPos);
            int top = itemPos[1] - listPos[1];
            if (top < 0) {
                listView.smoothScrollBy(top, 10);
            } else {
                int listY = listPos[1] + listView.getMeasuredHeight();
                int itemY = view.getMeasuredHeight() + itemPos[1];
                int bottom = itemY - listY;
                if (bottom > 0) {
                    listView.smoothScrollBy(bottom, 10);
                }
            }
        }
    }
}
